package org.qydjk.console.controller.user;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.user.UserCapitalWater;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.console.vo.UserCapitalWaterBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lanan on 2017/12/18 0018.
 */
@Controller
@RequestMapping("/user/capital/flow")
public class UserCapitalFlowController extends BaseController {
    @RequestMapping("/index.do")
    public String userCapitalFlow() {
        return "user/capitalFlow/userCapitalFlow";
    }

    @RequestMapping("/list.do")
    @ResponseBody
    public Object userCapitalFlowList(int page, int rows) {
        List<UserCapitalWater> userCapitalWaters = new ArrayList<UserCapitalWater>();
        Integer total = 0;
        String sql = "SELECT ucw.*, u.username as user_name, u.phone as user_phone, u.email as user_email " +
                "FROM qy_user_capital_water ucw " +
                "LEFT JOIN qy_user u ON ucw.user_id = u.id " +
                "WHERE 1 = 1 ";
        String countSql = "SELECT COUNT(ucw.id) " +
                "FROM qy_user_capital_water ucw " +
                "LEFT JOIN qy_user u ON ucw.user_id = u.id " +
                "WHERE 1 = 1 ";
        // 添加userID条件
        sql += userIdHandel();
        countSql += userIdHandel();
        // 添加type条件
        sql += typeHandel();
        countSql += typeHandel();
        // 添加编号条件
        sql += numberHandel();
        countSql += numberHandel();
        // 添加时间条件
        sql += dateHandel();
        countSql += dateHandel();

        sql += " order by ucw.create_time desc";

        try {
            userCapitalWaters = baseService.findBeanBySQL(sql, UserCapitalWaterBean.class, new HibernatePage(page, rows));
            total = baseService.getRecordCountSQL(countSql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(total, userCapitalWaters);
    }

    private String userIdHandel() {
        String userID = request.getParameter("userId");
        if (!StringUtil.isEmptyOrNull(userID)) {
            String whereId = " AND ucw.user_id = " + userID;
            return whereId;
        }
        return "";
    }

    private String numberHandel() {
        String number = request.getParameter("number");
        if (!StringUtil.isEmptyOrNull(number)) {
            String whereNum = " AND (ucw.order_no LIKE '%" + number + "%'" +
                    " OR ucw.order_product_no LIKE '%" + number + "%'" +
                    " OR ucw.recharge_order_no LIKE '%" + number + "%')";
            return whereNum;
        }
        return "";
    }

    private String typeHandel() {
        String type = request.getParameter("type");
        if (!StringUtil.isEmptyOrNull(type)) {
            String whereType = " AND ucw.type = " + type;
            return whereType;
        }
        return "";
    }

    private String dateHandel() {
        String startDateStr = request.getParameter("searchStartTemp");
        String endDateStr = request.getParameter("searchEndTemp");
        boolean startdateIsEmpty = !StringUtil.isEmptyOrNull(startDateStr);
        boolean endDateIsEmpty = !StringUtil.isEmptyOrNull(endDateStr);
        String dateWhere = "";
        if (startdateIsEmpty || endDateIsEmpty) {
            if (startdateIsEmpty) {
                startDateStr += " 00:00:00";
                dateWhere += addDateStr(startDateStr, ">=");
            }
            if (endDateIsEmpty) {
                endDateStr += " 23:59:59";
                dateWhere += addDateStr(endDateStr, "<");
            }
        }
        return dateWhere;
    }

    private String addDateStr(String dateStr, String bj) {
        String whereEndTime = " AND ucw.create_time" + bj + "'" + dateStr + "'";
        return whereEndTime;
    }


}
